<template>
  <div id="details">
    <JfHeader :viewstyle="viewstyle"></JfHeader>
    <!-- 主要内容 -->
    <el-row class="main" style="height: 800px">
      <el-col :span="8" :offset="2" style="height: 800px">
        <div class="block">
          <div v-if="productDetails.pics">
            <img
              style="height: 560px; width: 100%"
              :src="$target + '/sharePath/' + productDetails.pics[0].pic"
            />
          </div>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="content">
          <h1 class="name">{{ productDetails.goods_name }}</h1>
          <div class="intro" v-html="productDetails.goods_introduce"></div>
          <div style="font-size: 8px; color: rgba(163, 163, 163, 0.8)">
            {{ productOnly }}
          </div>
          <p class="store">小米自营</p>
          <div>
            <el-radio-group
              class="radiogroup"
              v-for="(dynamic, index) in productMany"
              :key="index"
              v-model="checked[index]"
              @change="getValue()"
            >
              <span class="productName">{{ productname[index] }}</span>
              <el-radio-button
                v-for="(item, index) in dynamic"
                :key="'info1-' + index"
                :label="item"
                class="productRadio"
              ></el-radio-button>
            </el-radio-group>
          </div>
          <div class="pro-list">
            <span class="pro-name">{{ productDetails.goods_name }}</span>
            <span class="pro-price">
              <span>{{ productDetails.goods_price }}元</span>
            </span>
            <p class="price-sum">总计 : {{ productDetails.goods_price }}元</p>
          </div>
          <!-- 内容区底部按钮 -->
          <div class="button">
            <el-button class="shop-cart" @click="addShoppingCart"
              >加入购物车</el-button
            >
          </div>
          <!-- 内容区底部按钮END -->
          <div class="pro-policy">
            <ul>
              <li><i class="el-icon-circle-check"></i> 小米自营</li>
              <li><i class="el-icon-circle-check"></i> 小米发货</li>
              <li><i class="el-icon-circle-check"></i> 7天无理由退货</li>
              <li><i class="el-icon-circle-check"></i> 7天价格保护</li>
            </ul>
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- 主要内容END -->
    <JfFooter></JfFooter>
  </div>
</template>
<script>
import JfFooter from "@/components/JfFooter.vue";
import JfHeader from "@/components/JfHeader.vue";

export default {
  data() {
    return {
      viewstyle: "details",
      checked: [],
      dis: false, // 控制“加入购物车按钮是否可用”
      productID: "", // 商品id
      productDetails: "", // 商品详细信息
      productname: [], //商品动态参数属性
      productMany: [], // 商品动态参数
      productOnly: "", //商品静态参数
      attr: "", //发送的动态参数
    };
  },
  components: {
    JfFooter,
    JfHeader,
  },
  methods: {
    // 获取商品详细信息
    getDetails(val) {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0)",
      });
      this.$axios
        .get("goods/" + val, {})
        .then((res) => {
          console.log(res.data.data);
          var _this = this;
          this.productDetails = res.data.data;
          for (let item of res.data.data.attrs) {
            if (item.attr_sel == "many") {
              _this.productname.push(item.attr_name);
              _this.productMany.push(item.attr_vals.split(","));
            } else if (item.attr_sel == "only") {
              // item.attr_vals
              _this.productOnly += item.attr_vals;
            }
          }
          this.hanldGoodsData(_this.productMany);
          console.log(_this.productOnly);
          console.log(_this.productMany);
          loading.close();
        })
        .catch((err) => {
          console.log(err);
          loading.close();
        });
    },
    // 获取动态参数值
    getValue() {
      this.handleCheckLogin();
      console.log(this.checked);
      var _this = this;
      _this.attr = "";
      for (let item of this.checked) {
        console.log(this.checked.length);
        _this.attr += item + ",";
      }
      if (_this.attr.length > 0) {
        _this.attr = _this.attr.substr(0, _this.attr.length - 1);
      }
      console.log(_this.attr);
    },
    // 加入购物车
    addShoppingCart() {
      this.handleCheckLogin();
      this.getValue();
      if (this.attr == null || this.attr == "") {
        return this.$message.error("请选择类型");
      }
      this.$axios
        .post("cart/add", {
          goodsId: this.productID,
          num: 1,
          attr: this.attr,
        })
        .then((res) => {
          this.$router.push("/shop_s");
          console.log(res);
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    // 洗数据
    hanldGoodsData(res) {
      let max = 3;
      let list = [];
      if (res.length < max) {
        max = res.length;
      }
      for (let index = 0; index < max; index++) {
        list.push(res[index]);
      }
      this.productMany = list;
    },
    handleCheckLogin() {
      if (
        window.sessionStorage.getItem("token") == null ||
        window.sessionStorage.getItem("token") == ""
      ) {
        this.$message.error("请先登录");
        this.$router.push("/login");
        return true;
      }
    },
  },
  created() {
    if (this.$route.query.productID != undefined) {
      window.sessionStorage.setItem("productID", this.$route.query.productID);
      this.productID = this.$route.query.productID;
      this.getDetails(this.productID);
    } else if (window.sessionStorage.getItem("productID") !== null) {
      this.productID = window.sessionStorage.getItem("productID");
    } else {
      this.$message.error("物品已下架，请选择其他商品或者联系客服。");
      this.$router.push("/index");
    }
    console.log(this.productID);
  },
};
</script>
<style scoped>
/* 主要内容CSS */
#details {
  background-color: rgb(238, 238, 238);
}
#details .main {
  height: 560px;
  text-align: left !important;
}
#details .main .block {
  float: left;
  height: 560px;
}
#details .el-carousel .el-carousel__indicator .el-carousel__button {
  background-color: rgba(163, 163, 163, 0.8);
}
#details .main .content {
  margin-left: 25px;
}
#details .main .content .radiogroup {
  display: block;
}
#details .main .content .productName {
  width: 50px;
  height: 50px;
  line-height: 50px;
  font-size: 15px;
}
#details .main .content .productRadio {
  border: 1px solid #dcdfe6;
  margin-left: 10px;
  border-radius: 10px;
}
#details .main .content .productRadio span {
  border: 0;
  border-radius: 10px;
}
#details .main .content .name {
  height: 30px;
  line-height: 30px;
  font-size: 24px;
  font-weight: normal;
  color: #212121;
}
#details .main .content .intro {
  color: #b0b0b0;
  padding-top: 10px;
  font-size: 16px;
}
#details .main .content .only {
  white-space: wrap;
  text-overflow: ellipsis;
  -o-text-overflow: ellipsis;
  overflow: hidden;
  color: #b0b0b0;
  padding-top: 10px;
}
#details .main .content .store {
  color: #ff6700;
  padding-top: 10px;
}
#details .main .content .price {
  display: block;
  font-size: 18px;
  color: #ff6700;
  border-bottom: 1px solid #e0e0e0;
  padding: 25px 0 25px;
}
#details .main .content .price .del {
  font-size: 14px;
  margin-left: 10px;
  color: #b0b0b0;
  text-decoration: line-through;
}
#details .main .content .pro-list {
  background: #f9f9fa;
  padding: 30px 60px;
  margin: 2px 0 20px;
}
#details .main .content .pro-list span {
  line-height: 30px;
  color: #616161;
}
#details .main .content .pro-list .pro-price {
  float: right;
}
#details .main .content .pro-list .pro-price .pro-del {
  margin-left: 10px;
  text-decoration: line-through;
}
#details .main .content .pro-list .price-sum {
  color: #ff6700;
  font-size: 24px;
  padding-top: 20px;
}
#details .main .content .button {
  height: 55px;
  margin: 10px 0 20px 0;
}
#details .main .content .button .el-button {
  float: left;
  height: 55px;
  font-size: 16px;
  color: #fff;
  border: none;
  text-align: center;
}
#details .main .content .button .shop-cart {
  width: 634px;
  background-color: #ff6700;
}
#details .main .content .button .shop-cart:hover {
  background-color: #f25807;
}

#details .main .content .button .like {
  width: 260px;
  margin-left: 40px;
  background-color: #b0b0b0;
}
#details .main .content .button .like:hover {
  background-color: #757575;
}
#details .main .content .pro-policy li {
  float: left;
  margin-right: 20px;
  color: #b0b0b0;
}
/* 主要内容CSS END */
</style>
